package com.zjw.zy.base;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/30 14:02
 */
public class BaseDiffCallback<DATA> extends DiffUtil.Callback {

    private List<DATA> oldList;
    private List<DATA> newList;

    public void setData(@NonNull List<DATA> oldList, @NonNull List<DATA> newList) {
        this.oldList = new ArrayList<>(oldList);
        this.newList = new ArrayList<>(newList);
    }

    protected List<DATA> getOldList() {
        return oldList;
    }

    protected List<DATA> getNewList() {
        return newList;
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        return areItemsTheSame(oldList.get(oldItemPosition), newList.get(newItemPosition));
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        return areContentsTheSame(oldList.get(oldItemPosition), newList.get(newItemPosition));
    }

    protected boolean areContentsTheSame(DATA oldItem, DATA newItem){
        return oldItem.equals(newItem);
    }

    protected boolean areItemsTheSame(DATA oldItem, DATA newItem){
        return oldItem.equals(newItem);
    }

    @Override
    public int getOldListSize() {
        return oldList.size();
    }

    @Override
    public int getNewListSize() {
        return newList.size();
    }
}
